<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 7:48 PM
 */
declare(strict_types=1);


namespace application\module\admin\model;


use wshore\core\ActiveRecord;
use wshore\throws\DatabaseException;

/**
 * Class CalendarModel
 *
 * @property int $uid
 * @property int $start 开始时间
 * @property int $end 结束时间
 * @property string $title 提醒内容
 * @property string $updtime 更新时间
 * @property int $done 是否完成
 *
 * @package application\module\admin\model
 */
class CalendarModel extends ActiveRecord
{
    protected function tablePrefix()
    {
        return 'ws_';
    }

    protected function tableName()
    {
        return 'calendar';
    }

    private $uid = 0;

    public function __construct($uid = null)
    {
        parent::__construct();
        $this->uid = $uid;
    }

    /**
     * 获取客户列表
     * @return array
     */
    public function getlist(): array
    {
        $list = $this->where(['uid' => $this->uid])->select();
        return $list;
    }

    /**
     * @param string $title
     * @param int $start
     * @param int $end
     * @return bool
     * @throws DatabaseException
     */
    public function add(string $title, int $start, int $end): bool
    {
        $res = $this->insert([
            'uid' => $this->uid,
            'title' => $title,
            'start' => $start,
            'end' => $end,
        ]);
        return $res > 0;
    }

    /**
     * 删除历程
     * @param int $id
     * @return bool
     */
    public function rm(int $id): bool
    {
        $res = $this->delete([
            'uid' => $this->uid,# 非必需
            'id' => $id,
        ]);
        return $res > 0;
    }

    public function createStatement()
    {
        return "CREATE TABLE `naz_calendar` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(10) unsigned NOT NULL COMMENT 'user id',
  `title` varchar(255) NOT NULL COMMENT '提醒内容',
  `start` int(11) NOT NULL COMMENT '开始时间',
  `end` int(11) unsigned NOT NULL COMMENT '结束时间',
  `updtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `done` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '是否完成',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
    }
}